<?php
exit;
/**
 * @param unknown_type $str
 * 存在-并且 -后面不是字母
 * @return
 * 0 是单词
 * 1 单意词缀 1行之内完毕
 * 2 多行词缀
 * -1 空行
 */
$data=array();
function isFix($str){
	$str = trim($str);
	
	$pos = strpos($str, "-");
	if(strlen($str)==0){
		return -1;
		//die("空行");
	}
	if($pos!==false){
		if (!ctype_alpha(trim(substr($str,0,$pos)))){
			//是字母  为单词 不是词缀
			//echo trim(substr($str,0,$pos))."-------";
			return 0;
		}
		if ($pos==strlen($str)-1){//没有解释 多行
			return 2;
		}else{
		$w = substr($str,$pos+1,1);
			if (ctype_alpha($w)){//是字母  为单词 不是词缀
				return 0;
			}
			return 1;//有解释 是单行
		}
		
	}
	return 0;//无-为单词
}
//echo isFix("");
//exit;
/**
 * 
 * 整理单行词缀
 * @return 下一个词缀行数
 */
function fun1($i){
	
	global $lines,$data;

	$str=trim($lines[$i]);
	if (strstr($str, "-")){
		$tt = explode("-", $str);
		$root=$tt[0]."-";
		$p=trim(substr($str, strlen($tt[0])));
	}else{
		$tt = preg_split("/\s+/", $str);
		$root=$tt[0];
		$p=trim(substr($str, strlen($tt[0])));
	}
	
	
	for ($i++;;$i++){
		$str=trim($lines[$i]);
		
		if(isFix($str)!=0){
			//是词缀 终止
			//print_r($array);
			//echo $i." aa\n";
			return $i;
		}else{
			$tt = preg_split("/\s+/", $str);
			$data[$root][$p][]=array($tt[0],trim(substr($str, strlen($tt[0]))));
		
		}
	}
}
/**
*
* 整理多行词缀
* @return 下一个词缀行数
*/
function fun2($i){
	global $lines,$tNum,$data;
	$root = trim($lines[$i]);//词根
	//echo $root;
	$p="";
	
	for ($i++;;$i++){
		$str=trim($lines[$i]);
		if(isFix($str)>0){
			//是词缀 终止
			//print_r($array);
			return $i;
		}else{
			
			if(is_numeric(substr($str, 0,1))){//1、。。。解释
				$p=substr($str,4);
			}else{
				//echo $str;
				$tt = preg_split("/\s+/", $str);				
				$data[$root][$p][]=array($tt[0],trim(substr($str, strlen($tt[0]))));
			}
		}
	}
	
}
$dir=__DIR__."/qz";
$contents= file_get_contents($dir);
$contents=trim($contents);
$lines=explode("\n", $contents);
$tNum = count($lines);
for ($i=0;$i<$tNum;){//echo $i."\n";
	$isFix =isFix($lines[$i]);
	
	if ($isFix==1){
		$i=fun1($i);
	}elseif ($isFix==2){
		$i=fun2($i);	
	}elseif ($i<$tNum){
		die("错误发生").$i;
	}
}
echo "读取完毕";
print_r(array_keys($data));
print_r(($data));
echo count($data);
mysql_connect("localhost","root","");
mysql_select_db("diandi_danci");
mysql_query("set names utf8");
foreach ($data as $root=> $row){
	mysql_query("INSERT INTO `diandi_danci`.`affix` (`id`, `type`, `affix`, `info`) VALUES (NULL, '0', '".$root."', '');");
	$rootid= mysql_insert_id();
	foreach ($row as $info=>$lines){
		mysql_query("INSERT INTO `diandi_danci`.`affix_info` (`id`, `affix_id`, `info`) VALUES (NULL, '".$rootid."', '".$info."');");
		$infoRootId=mysql_insert_id();
		foreach ($lines as $word){
			//$word[0]//1
			$re = mysql_query("select * from word where word='".$word[0]."'");
			if ($r=mysql_fetch_array($re)){
				$wordId=$r['id'];
			}else{
				mysql_query("INSERT INTO `diandi_danci`.`word` (`id`, `word`, `symbolA`, `symbolE`, `pronunciationA`, `pronunciationE`) VALUES (NULL, '".$word[0]."', '', '', '', '');");
				$wordId=mysql_insert_id();
			}
			mysql_query("INSERT INTO `diandi_danci`.`word_affix` (`id`, `word_id`, `affix_id`, `info`, `affix_info_id`) VALUES (NULL, '".$wordId."', '".$rootid."', '".$word[1]."', '".$infoRootId."');");
		}
	}
}